//跑马灯UI类;
import uiPanel from "./../lib/UiPanel";
import RenderNode from "../lib/RenderNode/RenderNode";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Marquee extends uiPanel {
	@property(cc.Node)
	marquee: cc.Node = null;

	//作为一个精灵,用来显示富文本的结果
	@property(cc.Sprite)
	content: cc.Sprite = null;

	@property(cc.Label)
	lbl_txt: cc.Label = null;

	/**真实的富文本节点,  render后在 this.content 上显示 */
	@property(cc.Node)
	in_node: cc.Node = null;

	@property(cc.Node)
	pn_holder: cc.Node = null;

	runTime = 0;
	speed = 0;
	callBack = null;
	_data = null;
	_is_running = false;
	onLoad() {
		this.runTime = 10;
		this.speed = 100;
	}

	reset(name) {
		this.content.node.x = 10;
		this.setRichString("");

		this._is_running = false;

		name = name || cc.vv.sceneName;
		var _pos = cc.vv.GLConfig.getMarqueePos(name);
		if (_pos) {
			this.marquee.x = _pos.x;
			this.marquee.y = _pos.y;
		}
		console.log("____marquee.x = " + this.marquee.x);
		console.log("____marquee.y = " + this.marquee.y);
	}
	active(flag) {
		this.node.active = flag;
	}
	run(data, callBack) {
		this.callBack = callBack;
		this._data = data;
		this._is_running = true;
		// this.marquee.stopAllActions();
		this.marquee.opacity = 255;
		this.setRichString("");
		this.content.node.x = 10;
		if (this._data && this._data.content) {
			// if (cc.vv.sceneName === "Hall" && this._data.msgType != 1) {
			// 	this._is_running = false;
			// 	this.active(false);
			// } else {
			// this.content.string = this._data.content;
			this.active(true);
			this.setRichString(this._data.content);
			//}

		} else {
			this._is_running = false;
			this.active(false);
		}
	}

	update(dt) {
		if (this._is_running) {
			this.content.node.x -= dt * this.speed;
			if (
				this.content.node.x < -(this.content.node.width + this.pn_holder.width)
			) {
				this._is_running = false;
				this.end();
				// var fadeOutAct = cc.fadeOut(0.3);
				// var callFunc = cc.callFunc(this.end, this);
				// var sequence = cc.sequence(fadeOutAct, callFunc);
				// this.marquee.runAction(sequence);
				// if (this.callBack) {
				// 	this.callBack();
				// }
			}
		}
	}

	end() {
		this.active(false);
	}

	Running() {
		return this._is_running;
	}


	setRichString(str) {
		this.in_node.active = true;
		this.lbl_txt.string = str;
		//调用一次手动渲染(lbl 宽高不是实时变化)
        this.lbl_txt[`_forceUpdateRenderData`](true);
		if (str != "") {
			// this.content.node.removeAllChildren();
			// if (cc.vv.MarqueeDataModel.getNeedRender()) {
			// this.node.active = true;
			this._renderTexture(this.content, this.in_node);
			// } else {
			// this.node.active = false;
			// this._is_running = false;
			// this.in_node.active = false;
			// this.content.spriteFrame = null;
			// let chd = cc.instantiate(this.in_node);
			// chd.parent = this.content.node;
			// chd.setPosition(cc.v2(0, 0));
			// chd.active = true;
			// }
		}

	}
    /**
     * 
     * @param out_sprite 输出显示的精灵
     * @param in_node 输入截图的节点
     */
	_renderTexture(out_sprite: cc.Sprite, in_node: cc.Node) {
		RenderNode.renderNode(out_sprite, in_node);
	}
}
